package 求根节点到叶节点数字之和;

import tree_node.TreeNode;

class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    public int dfs(TreeNode root, int presum){
        int sum = root.val + presum * 10;
        if(root.left == null && root.right == null){
            return sum;
        }
        if (root.left == null){
            return dfs(root.right, sum);
        }
        if (root.right == null){
            return dfs(root.left, sum);
        }
        int left = dfs(root.left, sum);
        int right = dfs(root.right, sum);
        return left + right;
    }
}